home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / dev / gui / FoxGuiSource.lha / FoxLibSource / foxconsole.c < prev    next >
C/C++ Source or Header  |  2001-07-07  |  8KB  |  274 lines

  1. /* FoxGUI - The fast, flexible, free Amiga GUI system
  2.     Copyright (C) 2001 Simon Fox (Foxysoft)
  3.  
  4. This library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  5. Foxysoft: www.foxysoft.co.uk      Email:simon@foxysoft.co.uk                */
  6.  
  7. /******************************************************************************
  8.  * Shared library code.  Cannot call functions which use exit() such as:
  9.  * printf(), fprintf()
  10.  *
  11.  * Otherwise:
  12.  * The linker returns "__XCEXIT undefined" and the program will fail.
  13.  * This is because you must not exit() a library!
  14.  *
  15.  * Also:
  16.  * proto/exec.h must be included instead of clib/exec_protos.h and
  17.  * __USE_SYSBASE must be defined.
  18.  *
  19.  * Otherwise:
  20.  * The linker returns "Absolute reference to symbol _SysBase" and the
  21.  * library crashes.  Presumably the same is true for the other protos.
  22.  ******************************************************************************/
  23.  
  24. #define __USE_SYSBASE
  25.  
  26. #include <proto/mathieeedoubbas.h>
  27. #include <stdio.h>
  28. #include <ctype.h>
  29.  
  30. #include <intuition/intuition.h>
  31. #include <proto/exec.h>
  32. #include <clib/alib_protos.h>
  33. #include "/FoxInclude/foxgui.h"
  34.  
  35. static int error;
  36.  
  37. // Copied from GuiSys.h
  38. extern void GuiSetLastErrAndLine(char *error, char *file, int line);
  39.  
  40. #define SetLastErr(a,n)           GuiSetLastErrAndLine(a,__FILE__,__LINE__);error=n
  41.  
  42. static void ErrorConsole(struct Console *con)
  43. {
  44.    if (error < 1) CloseDevice((struct IORequest*) con->ConOut);
  45.    if (error < 2) DeleteExtIO((struct IORequest*) con->ConIn);
  46.    if (error < 3) DeletePort(con->RePort);
  47.    if (error < 4) DeleteExtIO((struct IORequest*) con->ConOut);
  48.    if (error < 5) DeletePort(con->WrPort);
  49. }
  50.  
  51. void FOXLIB CloseConsole(register __a0 struct Console *con)
  52. {
  53.     error = 0;
  54.    ErrorConsole(con);
  55. }
  56.  
  57. int FOXLIB OpenConsole(register __a0 struct Console *con, register __a1 struct Window *win, register __a2 char *name)
  58. {
  59.     char str[20];
  60.     error = 0;
  61.     sprintf(str, "%sOutCon", name);
  62.     if ((con->WrPort = /*(struct MsgPort *)*/ CreatePort(str, 0)) != 0)
  63.     {
  64.         if ((con->ConOut = (struct IOStdReq*) CreateExtIO(con->WrPort, sizeof(struct IOStdReq))) != 0)
  65.         {
  66.             sprintf(str, "%sInCon", name);
  67.             if ((con->RePort = /*(struct MsgPort *)*/ CreatePort(str, 0)) != 0)
  68.             {
  69.                 if ((con->ConIn = (struct IOStdReq*) CreateExtIO(con->RePort, sizeof(struct IOStdReq))) != 0)
  70.                 {
  71.                     con->ConOut->io_Data = (APTR) win;
  72.                     con->ConOut->io_Length = sizeof(struct Window);
  73.                     if (OpenDevice("console.device", 0, /*(struct IORequest*)*/ con->ConOut, 0) == 0)
  74.                     {
  75.                         con->ConIn->io_Device = con->ConOut->io_Device;
  76.                         con->ConIn->io_Unit = con->ConOut->io_Unit;
  77.                     }
  78.                     else
  79.                         SetLastErr("Failed to open console device in OpenConsole.", 1);
  80.                 }
  81.                 else
  82.                     SetLastErr("Failed to create input IOStdReq in OpenConsole.", 2);
  83.             }
  84.             else
  85.                 SetLastErr("Failed to create input port in OpenConsole.", 3);
  86.         }
  87.         else
  88.             SetLastErr("Failed to create output IOStdReq in OpenConsole.", 4);
  89.     }
  90.     else
  91.         SetLastErr("Failed to create output port in OpenConsole.", 5);
  92.     if (error > 0)
  93.     {
  94.         ErrorConsole(con);
  95.         return FALSE;
  96.     }
  97.     return TRUE;
  98. }
  99.  
  100. void FOXLIB ConPutChar(register __a0 struct Console *con, register __d0 char ch)
  101. {
  102.     char ch1 = ch;
  103.  
  104.    con->ConOut->io_Command = CMD_WRITE;
  105.    con->ConOut->io_Data = (APTR) &ch1;
  106.    con->ConOut->io_Length = 1;
  107.    DoIO((struct IORequest*) con->ConOut);
  108.    if (ch == VAL_CR) ConPutChar(con, VAL_LF);
  109. }
  110.  
  111. void FOXLIB QueueRead(register __a0 struct Console *con, register __a1 UBYTE *whereto)
  112. {
  113.    con->ConIn->io_Command = CMD_READ;
  114.    con->ConIn->io_Data = (APTR) whereto;
  115.    con->ConIn->io_Length = 1;
  116.    SendIO((struct IORequest*) con->ConIn);
  117. }
  118.  
  119. LONG FOXLIB ConMayGetChar(register __a0 struct Console *con, register __a1 UBYTE *whereto)
  120. {
  121.    register UBYTE temp;
  122.  
  123.    struct Console newcon;
  124.    if (!(newcon.ConIn = (struct IOStdReq *) GetMsg(con->RePort)))
  125.       return -1;
  126.    temp = *whereto;
  127.    QueueRead(&newcon, whereto);
  128.    return temp;
  129. }
  130.  
  131. static UBYTE ConGetCh(struct Console *con, UBYTE *whereto)   /* ConGetChar */
  132. {
  133.    register UBYTE temp;
  134.  
  135.    WaitPort(con->RePort);
  136.    con->ConIn = (struct IOStdReq *) GetMsg(con->RePort);
  137.    temp = *whereto;
  138.    QueueRead(con, whereto);
  139.    return((UBYTE) temp);
  140. }
  141.  
  142. char FOXLIB ConGetChar(register __a0 struct Console *con, register __a1 UBYTE *ibuf)    /* wgetc */
  143. {
  144.    UBYTE ch;
  145.  
  146.    ch = ConGetCh(con, ibuf);
  147.    return ((char) ch);
  148. }
  149.  
  150. void FOXLIB ConPrint(register __a0 struct Console *con, register __a1 char *String)  /* wprintf */
  151. {
  152.    con->ConOut->io_Command = CMD_WRITE;
  153.    con->ConOut->io_Data = (APTR) String;
  154.    con->ConOut->io_Length = -1;
  155.    DoIO((struct IORequest*) con->ConOut);
  156. }
  157.  
  158. void FOXLIB ConClear(register __a0 struct Console *con)        /* wclear */
  159. {
  160.    ConPutChar(con, 12);
  161. }
  162.  
  163. void FOXLIB ConHome(register __a0 struct Console *con)         /* whome */
  164. {
  165.    ConPrint(con, "\033[H");
  166. }
  167.  
  168. void FOXLIB ConBlankToEOL(register __a0 struct Console *con)   /* wblanktoeol */
  169. {
  170.    ConPrint(con, "\033[K");
  171. }
  172.  
  173. void FOXLIB ConTab(register __a0 struct Console *con, register __d0 int x, register __d1 int y)    /* wtab */
  174. {
  175.    char str[10];
  176.    sprintf(str, "\033[%d;%dH", y, x);
  177.    ConPrint(con, str);
  178. }
  179.  
  180. void FOXLIB ConPrintTab(register __a0 struct Console *con, register __d0 int x, register __d1 int y, register __a1 char *str)
  181. {
  182.    char nstr[200];
  183.    sprintf(nstr, "\033[%d;%dH%s", y, x, str);
  184.    ConPrint(con, nstr);
  185. }
  186.  
  187. static int ConGetNum(struct Console *con, UBYTE *buf)   /* wGetNum */
  188. {
  189.    int num = 0, ptr = 0, neg = 1;
  190.    char string[30], ch;
  191.  
  192.    while ((ch = ConGetChar(con, buf)) != VAL_CR)
  193.    {
  194.       if (ch == VAL_BS)
  195.       {
  196.          if (ptr > 0)
  197.          {
  198.             ConPutChar(con, ch);
  199.             ConPutChar(con, ' ');
  200.             ConPutChar(con, ch);
  201.             ptr--;
  202.          }
  203.       }
  204.       else
  205.       {
  206.          if (ptr < 29)
  207.          {
  208.             ConPutChar(con, ch);
  209.             string[ptr++] = ch;
  210.          }
  211.       }
  212.    }
  213.    ConPutChar(con, VAL_CR);
  214.    string[ptr] = '\0';
  215.    ptr = 0;
  216.    if (string[ptr] == '-')
  217.    {
  218.       neg = -1;
  219.       ptr++;
  220.    }
  221.    while (isdigit(string[ptr]))
  222.    {
  223.       num *= 10;
  224.       num += string[ptr++] - '0';
  225.    }
  226.    num *= neg;
  227.    return num;
  228. }
  229.  
  230. static void ConSelectEvent(struct Console *con, int EventType)
  231. {
  232.    char out[5];
  233.  
  234.    sprintf(out, "\033[%d{", EventType);
  235.    if (EventType >= 0 && EventType <= 16)
  236.       ConPrint(con, out);
  237. }
  238.  
  239. // Turn auto-wrap OFF for the given console (default is ON)
  240. void FOXLIB ConWrapOff(register __a0 struct Console *con)
  241. {
  242.     char WrapOff[5];
  243.  
  244.     sprintf(WrapOff, "%c?7l", 155);
  245.     ConPrint(con, WrapOff);
  246. }
  247.  
  248. // Turn auto-wrap ON for the given console (default is ON)
  249. void FOXLIB ConWrapOn(register __a0 struct Console *con)
  250. {
  251.     char WrapOn[5];
  252.  
  253.     sprintf(WrapOn, "%c?7h", 155);
  254.     ConPrint(con, WrapOn);
  255. }
  256.  
  257. void FOXLIB ConHideCursor(register __a0 struct Console *con)
  258. {
  259.    ConPrint(con, "\033[0 p");
  260. }
  261.  
  262. void FOXLIB ConShowCursor(register __a0 struct Console *con)
  263. {
  264.    ConPrint(con, "\033[ p");
  265. }
  266.  
  267. void FOXLIB ConPrintHi(register __a0 struct Console *con, register __a1 char *text, register __d0 int col)
  268. {
  269.    char temp[200];
  270.  
  271.    sprintf(temp, "\033[3%-dm%s\033[0m", col, text);
  272.    ConPrint(con, temp);
  273. }
  274.